package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Certification;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户业务处理
 **/
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;
    @Autowired
    private CertificationService certificationService;

    /**
     * 新增
     *
     * @param user
     */
    public void add(User user) {
        User user1 = this.selectByUsername(user.getUsername());
        if (ObjectUtil.isNotNull(user1)) {
            //  用户名重复
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(user.getPassword())) {
            // 默认密码
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(user.getName())) {
            user.setName(user.getUsername());
        }
        // 默认角色
        user.setRole(RoleEnum.USER.name());

        userMapper.insert(user);
    }

    public User selectByUsername(String username) {
        User params =new User();
        params.setUsername(username);
        List<User> userList= userMapper.selectAll(params);
        return  userList.size() == 0 ? null : userList.get(0);
    }


    /**
     * 删除单条数据
     *
     * @param id
     */
    public void deleteById(Integer id) {
        userMapper.deleteById(id);

    }

    public void deleteBatch(List<Integer> ids) {
        for ( int id : ids ){
            this.deleteById(id);
        }

    }

    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * id查询详细数据
     *
     * @param id
     */
    public User selectById(Integer id) {

        return userMapper.selectById(id);
    }

    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);

    }

    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectAll(user);
        return PageInfo.of(userList);
    }

    public Account login(Account account) {
        Account dbUser = userMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
        dbUser.setToken(token);

        Certification certification = certificationService.selectByUserId(dbUser.getId());  // 查询当前登录用户的骑手认证信息
        dbUser.setIsRider(ObjectUtil.isNotNull(certification) && "通过".equals(certification.getStatus()));

        return dbUser;
    }

    /**
     * 注册
     */
    public void register(Account account) {
        User user = new User();
        BeanUtils.copyProperties(account, user);
        this.add(user);
    }
}
